﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1TestTest.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";
            string s1 = GetIp();
            string s2 = GetIp2();
            string s3 = GetIp3();
            this.Response.Write(s1+"<br>");
            this.Response.Write(s2 + "<br>");
            this.Response.Write(s3 + "<br>");
            var s =System.Web.HttpContext.Current.Request.ServerVariables;
            foreach (string item in s.Keys)
            {
                this.Response.Write("键:"+item+"值:"+s[item]+"<br/>");

            }

            return View();
        }

        public ActionResult About()
        {
            return View();
        }


        /**/
        /// <summary>
        /// 获取用户登陆IP
        /// </summary>
        /// <returns>返回用户IP</returns>
        public string GetIp()
        {
            string user_IP;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            return user_IP;
        }

        /**/
        /// <summary>
        /// 获取用户登陆IP
        /// </summary>
        /// <returns>返回用户IP</returns>
        public string GetIp2()
        {
            return System.Web.HttpContext.Current.Request.ServerVariables.GetValues("REMOTE_ADDR")[0];
        }

        /**/
        /// <summary>
        /// 获取用户登陆IP
        /// </summary>
        /// <returns>返回用户IP</returns>
        public string GetIp3()
        {
            return System.Web.HttpContext.Current.Request.UserHostAddress;
        }
    }
}
